import { Component, OnInit, ViewChild } from '@angular/core';
import { ColumnType, SPage, TablePageComponent } from '@shared/components/table-page/table-page.component';
import { _HttpClient, ModalHelper } from '@delon/theme';
import { NzMessageService, NzModalService } from 'ng-zorro-antd';
import { ApiService } from '@core/services/api';
import { ResultComponent, STData,STRes } from '@delon/abc';
import { UtilService } from '@core/services/util.service';
@Component({
  selector: 'app-business-list',
  templateUrl: './business-list.component.html',
  styles: []
})
export class BusinessListComponent implements OnInit {
  @ViewChild('st', { static: false }) st: TablePageComponent;
  selectData: any[];
  loading = false;
  showTable:boolean = false;
  config: SPage = {
    columns: [
      {
        index: 'id',
        title: '编号',
        type:ColumnType.no,
        width:80,
      },
      {
        index: 'days',
        title: '日期',
        width:200,
        type:ColumnType.tag,
        filter: {
          index:"communityId",
          type:'select',
          placeholder:"所属社区",
          items:[],
        }
      },
      {
        index: 'newBan',
        title: '新办(元)',
        fFn:(item, columu)=>{
          return item.newBan / 100 + "";
        },
        type: ColumnType.tag,
        statistical: 'sum',
        key: 'newBan',
        filter: {
          index:"areaId",
          placeholder: '所属小区',
          type: 'select',
          items:[]
        }
      },
      {
        index: 'optionBan',
        title: '续期(元)',
        fFn:(item, columu)=>{
          return item.optionBan / 100 + "";
        },
        type: ColumnType.tag,
        statistical: 'sum',
        key: 'optionBan',
        filter: {
          index:"startDate",
          placeholder: '支付时间(起)',
          type: 'date'
        }
      },
      {
        index: 'reapplyBan',
        title: '补办(元)',
        fFn:(item, columu)=>{
          return item.reapplyBan / 100 + "";
        },
        type: ColumnType.tag,
        statistical: 'sum',
        key: 'reapplyBan',
        filter: {
          index:"endDate",
          placeholder: '支付时间(止)',
          type: 'date'
        }
      },
      {
        index: 'loginoutFee',
         title: '注销（已退费/元）',
         fFn:(item, columu)=>{
          return item.loginoutFee / 100 + "";
        },
        type: ColumnType.tag,
        statistical: 'sum',
        key: 'loginoutFee',
         filter: {
           index:"createBy",
          placeholder: '办理人',
          type: 'string'
        }
      },
      {
        index: 'payTime',
        title: '注销(未退费/元)',
        type: ColumnType.tag,
        statistical: 'sum',
        key: 'payTime',
        filter:{
          index:"createId",
          type:ColumnType.checkbox,
          items:[
            {label:"只统计当前账号产生的费用",value:"1"}
          ]
       }
      },
    ],
    showCheckBox: false,
    reqUrl:this.api.pay.payStatistics.url,
    defaultParam:{"statisticsType":"1","userType":JSON.parse(this.util.getStorage("userNow")).userType}
  }
  constructor(private http: _HttpClient,  private api: ApiService, private modal: NzModalService, private message: NzMessageService,private util:UtilService) { }
  ngOnInit() { 
    this.getArea().then(res=>{
      this.feachCommunity();
    })
  }

  dataProcess(data: STData[]){
    return data;
  }

  _console(e: any): void {
    this.selectData = e;
  }

    //获取小区下拉框数据
    feachCommunity(){
      this.http.post(this.api.area.list.url, {areaName: '',areaRank:6}).subscribe((res: any) => {
       if (res.status == '200') {
         this.config.columns[2].filter.items= res.data.map(item => {
           return {
             label: item.areaName,
             value: item.id
           }
         });
         this.showTable=true;
       }
     })
   }
   //获取社区下拉框数据
   getArea(){
    return new Promise<void>((resolve) => {
      this.http.post(this.api.area.page.url, {areaRank: '5'}).subscribe((res: any) => {
        console.log(res)
        if (res.status == '200') {
          if(res.data.list.length > 0){
            this.config.columns[1].filter.items= res.data.list.map(item => {
              return {
                label: item.areaName,
                value: item.id
              }
            });
          }
          resolve();
        }
      })
    }) 
   }

    //导出
    export(){
      this.loading=true;
      let body:any={};
      if(this.st.st.req.params != undefined){
        body=this.st.st.req.params;
      }
      body.pageNum=1;
      body.pageSize=this.st.st.total;
      this.http.post(this.api.pay.payStatistics.url,body).subscribe(res => {
        this.loading=false;
        if(res.status == 200 && res.data.list.length > 0){
          let data=res.data.list;
           this.st.st.export(data,{
            filename : "办理记录.xlsx"
          })
        }else{
          this.message.error(res.message);
        }
      })
    }

  //按日统计
  staticByDay(){
    let res=this.getParams(this.st.sf.value);
    this.st.st.reset({"statisticsType":"1",...res});
  }
  //按月统计
  staticByMonth(){
    let res=this.getParams(this.st.sf.value);
    this.st.st.reset({"statisticsType":"0",...res});
  }

  getParams(res){
    let result=res;
    let data:any=this.util.getStorage("userNow");
    data=JSON.parse(data);
    Object.keys(result).forEach(item=>{
      if(Array.isArray(result[item])){
        if(result[item].length != 0){
          result[item]=data.id;
        }else{
          delete result[item];
        }
      }
    })
    return result;
  }
  

}
